<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="text" placeholder="用户名/电话"></wInput>
				<wInput v-model="passData" type="password" placeholder="密码"></wInput>
			</view>
			<wButton text="登 录" :rotate="isRotate" @click.native="startLogin()" class="wbutton"></wButton>
			<!-- <button
        class="sys_btn"
			
        lang="zh_CN"
        @getuserinfo="appLoginWx"
      >
        {{
          loginInfo.openid != "" && loginInfo.openid != undefined
            ? "已授权"
            : "小程序授权"
        }}
      </button> -->

			<!-- <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
        获取电话号码
      </button> -->

			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from "../../components/watch-login/watch-input.vue"; //input
	import wButton from "../../components/watch-login/watch-button.vue"; //button
	import {
		setState
	} from "../../../store";
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: "",
				phoneData: "321071784@qq.com", // 用户/电话
				passData: "123456", //密码
				isRotate: false, //是否加载旋转
			};
		},
		components: {
			wInput,
			wButton,
		},
		onLoad() {
			uni.removeStorage({
				key: 'userInfo',
				success: function(res) {

				}
			});
		},
		methods: {
			isLogin() {},
			getPhoneNumber: function(e) {
				// console.log(e);
				// if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
				// } else {
				// }

				// 				console.log(JSON.stringify(e.encryptedData));
				// 				console.log(JSON.stringify(e.iv));
			},
			appLoginWx() {
				// #ifdef MP-WEIXIN
				// uni.getProvider({
				//   service: "oauth",
				//   success: function (res) {
				//     console.log(res);
				//     if (~res.provider.indexOf("weixin")) {
				//       uni.login({
				//         provider: "weixin",
				//         success: (res) => {
				//           // _self.authorization = res.code;
				//           uni.getUserInfo({
				//             provider: "weixin",
				//             success: (info) => {
				//               //这里请求接口
				//               console.log(res);
				//               console.log(info);
				//             },
				//             fail: () => {
				//               uni.showToast({
				//                 title: "微信登录授权失败",
				//                 icon: "none",
				//               });
				//             },
				//           });
				//         },
				//         fail: () => {
				//           uni.showToast({
				//             title: "微信登录授权失败",
				//             icon: "none",
				//           });
				//         },
				//       });
				//     } else {
				//       uni.showToast({
				//         title: "请先安装微信或升级版本",
				//         icon: "none",
				//       });
				//     }
				//   },
				// });
				//#endif
			},
			async startLogin() {
				// uni.navigateTo({
				//   url: `../index/index`,
				// });
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "用户名不能为空",
					});
					return;
				}
				if (this.passData.length < 5) {
					uni.showToast({
						icon: "none",
						position: "bottom",
						title: "密码不正确",
					});
					return;
				}
				let res = await this.http(this.api.login, {
					uerMail: this.phoneData,
					uerPwd: this.passData,
				});
				uni.showLoading({
					title: "登录中",
				});
				if (res) {
					uni.showToast({
						icon: "success",
						position: "bottom",
						title: "登录成功",
					});
					try {
						setState("user", "userInfo", res);
						uni.setStorageSync('userInfo', res);
						// uni.navigateTo({
						//   url: `pages/ivew/index/index`,
						// });
						uni.switchTab({
							url: `../index/index`,
							complete(e) {
								console.log(e);
							},
							fails(e) {
								console.log(e);
							}
						});
					} catch (e) {
						// error
					}

				}
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: "none",
					position: "bottom",
					title: "...",
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: "none",
					position: "bottom",
					title: "...",
				});
			},
			login_github() {
				// window.localStorage.preventHref = window.location.href
				// window.location.href = 'https://github.com/login/oauth/authorize?client_id=***&redirect_uri=http://biaochenxuying.cn/
				// github登录
				// uni.showToast({
				// 	icon: 'none',
				// 	position: 'bottom',
				// 	title: '...'
				// });
			},
		},
	};
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
